/**
 * 
 */
package com.java.test.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.util.Properties;
import java.util.UUID;
import java.util.Vector;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.java.test.system.feesettings.dto.FeeSettingsDTO;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

import lombok.Data;

/**
 * @author ftp 上传工具类
 *
 */
@Component
@Data
public class FtpJSch {
	
	private static ChannelSftp sftp = null;


	
	//主机ip
	@Value("${ftp.link.ip}")
	private  String host;
	
	//端口
	@Value("${ftp.link.port}")
	private  String port;
	
	//账号
	@Value("${ftp.link.username}")
	private  String user;
	
	//密码
	@Value("${ftp.link.password}")
	private  String password;

	//上传地址
	@Value("${ftp.link.file.upload.url}")
	private  String directory;
	
	//下载目录
	private static String saveFile = "";
	
	private static Channel channel=null;
	
	private static Session sshSession =null;
	

	
	public  FtpJSch getConnect(){
		FtpJSch ftp = new FtpJSch();
		try {
			JSch jsch = new JSch();
 
			//获取sshSession  账号-ip-端口
			sshSession =jsch.getSession(this.getUser(), this.getHost(),Integer.valueOf(this.getPort()));
			//添加密码
			sshSession.setPassword(password);
			Properties sshConfig = new Properties();
			//严格主机密钥检查
			sshConfig.put("StrictHostKeyChecking", "no");
			
			sshSession.setConfig(sshConfig);
			//开启sshSession链接
			sshSession.connect();
			//获取sftp通道
			channel = sshSession.openChannel("sftp");
			//开启
			channel.connect();
			sftp = (ChannelSftp) channel;
			System.out.println("链接成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ftp;
	}
	
	
    public void exitFtp(){
        if(channel != null){
            ((ChannelSftp) channel).quit();
            channel.disconnect();
        }
        if(sshSession != null){
            sshSession.disconnect();
        }
    }
	
	
	
	
	/**
	 * 
	 * @param uploadFile 上传文件的路径
	 * @return 服务器上文件名
	 */
	public String upload(File uploadFile) {
		File file = null;
		String fileName = null;
		try {
			sftp.cd(directory);
			file = uploadFile;
			//获取随机文件名
			fileName  = UUID.randomUUID().toString() + file.getName().substring(file.getName().length()-5);
			//文件名是 随机数加文件名的后5位
			sftp.put(new FileInputStream(file), fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return file == null ? null : fileName;
	}
	
	public static void main(String[] args) {
		String str="ABCD123456789";
		System.out.println(str.substring(str.length()-5));
	}
	
	/**
	 * 下载文件
	 * 
	 * @param directory
	 *            下载目录
	 * @param downloadFile
	 *            下载的文件名
	 * @param saveFile
	 *            存在本地的路径
	 * @param sftp
	 */
	public void download(String downloadFileName) {
		try {
			sftp.cd(directory);
			
			File file = new File(saveFile);
			
			sftp.get(downloadFileName, new FileOutputStream(file));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除文件
	 * 
	 * @param deleteFile
	 *            要删除的文件名字
	 * @param sftp
	 */
	public void delete(String deleteFile) {
		try {
			sftp.cd(directory);
			sftp.rm(deleteFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 列出目录下的文件
	 * 
	 * @param directory
	 *            要列出的目录
	 * @param sftp
	 * @return
	 * @throws SftpException
	 */
	@SuppressWarnings("rawtypes")
	public Vector listFiles(String directory)
			throws SftpException {
		return sftp.ls(directory);
	}


	public static ChannelSftp getSftp() {
		return sftp;
	}


	public static void setSftp(ChannelSftp sftp) {
		FtpJSch.sftp = sftp;
	}


	public static String getSaveFile() {
		return saveFile;
	}


	public static void setSaveFile(String saveFile) {
		FtpJSch.saveFile = saveFile;
	}


	public static Channel getChannel() {
		return channel;
	}


	public static void setChannel(Channel channel) {
		FtpJSch.channel = channel;
	}


	public static Session getSshSession() {
		return sshSession;
	}


	public static void setSshSession(Session sshSession) {
		FtpJSch.sshSession = sshSession;
	}

	
	
	
	
}
